﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Rung.EBook.UI.Controls.Panels;

namespace Rung.EBook.UI.Forms
{
    public partial class MainForm : Form
    {
        /* ---------------------------------------------------------------------------------------------Properties-------- */
        WriteInfo mWriteInfo;
        AppendInfo mAppendInfo;
        ProgressBarReset mProgressBarReset;
        ProgressBarIncreace mProgressBarIncreace;
        Timer tTimer;
        System.Diagnostics.PerformanceCounter mPerformanceCounter;

        /* ---------------------------------------------------------------------------------------------Constructor------- */
        public MainForm()
        {
            InitializeComponent();

            tTimer = new Timer();
            tTimer.Interval = 1000;
            tTimer.Tick += new EventHandler(tTimer_Tick);

            this.mPerformanceCounter = new System.Diagnostics.PerformanceCounter("Processor", "% Processor Time");
            this.mPerformanceCounter.InstanceName = "_Total";
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            mWriteInfo = WriteInfoListBox;
            mAppendInfo = AppendInfoListBox;
            mProgressBarReset = tsProgressBarReset;
            mProgressBarIncreace = tsProgressBarIncreace;

            tpDownloader.Controls.Add(new pnDownloader(mWriteInfo, mAppendInfo, mProgressBarReset, mProgressBarIncreace));
            tpEBookMaker.Controls.Add(new pnEBookMaker(mWriteInfo, mAppendInfo));
            tpProjectEdit.Controls.Add(new pnProjectEdit(mWriteInfo, mAppendInfo));
            tpBookReader.Controls.Add(new pnBookReader(mWriteInfo, mAppendInfo));
            tpChmMaker.Controls.Add(new pnChmMaker(mWriteInfo, mAppendInfo));
            tpTextAnalizer.Controls.Add(new pnTextAnalizer(mWriteInfo, mAppendInfo));
            
            tcMain.SelectedIndex = 3;

            tTimer.Start();
        }

        /* ---------------------------------------------------------------------------------------------Event Methods----- */
        // 退出
        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        // 设置
        private void btnConfig_Click(object sender, EventArgs e)
        {
        }

        // 绘制 lbInfo 数据项
        private void lbInfo_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index < 0) return;

            Brush brush = null;
            string item = lbInfo.Items[e.Index].ToString();

            if (item.StartsWith("[就绪]"))
            {
                brush = Brushes.DarkSlateGray;
            }
            else if (item.StartsWith("[执行]"))
            {
                brush = Brushes.Gray;
            }
            else if (item.StartsWith("[警告]"))
            {
                brush = Brushes.SaddleBrown;
            }
            else if (item.StartsWith("[错误]"))
            {
                brush = Brushes.Maroon;
            }
            //System.Drawing.Font bFont = new System.Drawing.Font(e.Font, FontStyle.Bold);

            e.DrawBackground();
            e.Graphics.DrawString(item, e.Font, brush, e.Bounds);
            e.DrawFocusRectangle();
        }

        // 清除
        private void btnClearInfo_Click(object sender, EventArgs e)
        {
            lbInfo.Items.Clear();
        }

        // “关于”窗口
        private void btnAbout_Click(object sender, EventArgs e)
        {
            new AboutForm().Show();
        }

        // 弹出测试窗口
        private void btnTest_Click(object sender, EventArgs e)
        {
            TestForm form = new TestForm();
            form.Show();
        }

        // 系统性能监视
        void tTimer_Tick(object sender, EventArgs e)
        {
            long memCost = System.Diagnostics.Process.GetCurrentProcess().PagedMemorySize64;
            tsslMemoryCost.Text = ((double)memCost / (1024 * 1024)).ToString(".#") + "M";
        }

        /* ---------------------------------------------------------------------------------------------Private Methods----- */
        // 显示消息
        private void WriteInfoListBox(string msg, InfoType type)
        {
            if (lbInfo.InvokeRequired)
            {
                MethodInvoker del = delegate { this.WriteInfoListBox(msg, type); };
                lbInfo.Invoke(del);
            }
            else
            {
                tsslInfo.Text = msg;

                switch (type)
                {
                    case InfoType.Success:
                        msg = "[就绪]" + msg;
                        break;
                    case InfoType.Process:
                        msg = "[执行]" + msg;
                        break;
                    case InfoType.Warning:
                        msg = "[警告]" + msg;
                        break;
                    case InfoType.Errer:
                        msg = "[错误]" + msg;
                        break;
                }

                lbInfo.Items.Add(msg);
                lbInfo.SelectedIndex = lbInfo.Items.Count - 1;
                lbInfo.SelectedIndex = -1;
            }
        }

        // 附加消息
        private void AppendInfoListBox(string msg)
        {
            if (lbInfo.InvokeRequired)
            {
                MethodInvoker mi = delegate { this.AppendInfoListBox(msg); };
                lbInfo.Invoke(mi);
            }
            else
            {
            lbInfo.Items[lbInfo.Items.Count - 1] = lbInfo.Items[lbInfo.Items.Count - 1].ToString() + msg;
        }
        }

        // 重置进度条
        internal void tsProgressBarReset(int max)
        {
            if (lbInfo.InvokeRequired)
            {
                MethodInvoker mi = delegate { this.tsProgressBarIncreace(); };
                lbInfo.Invoke(mi);
            }
            else
            {
                if (!tsProgressBar.IsDisposed)
                {
            tsProgressBar.Maximum = max;
            tsProgressBar.Value = 0;
        }
            }
        }

        // 进度条增量+1
        internal void tsProgressBarIncreace()
        {
            if (lbInfo.InvokeRequired)
            {
                MethodInvoker mi = delegate { this.tsProgressBarIncreace(); };
                lbInfo.Invoke(mi);
            }
            else
            {
                if (!tsProgressBar.IsDisposed)
                {
            if (tsProgressBar.Value < tsProgressBar.Maximum)
                tsProgressBar.Increment(1);
        }
    }
}
    }
}
